package com.zkingsoft.service.impl;

import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.constance.SystemErrorCode;
import com.zkingsoft.service.BizMaterialService;
import org.springframework.stereotype.Service;
import com.matrix.core.exception.GlobleException;
import com.zkingsoft.common.bean.SysUsers;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.dao.BizMaterialDao;
import org.springframework.beans.factory.annotation.Autowired;
import com.matrix.core.tools.ModelUtils;
import java.util.List;
import java.util.Map;
import com.zkingsoft.bean.BizMaterial;

/**
 * @description service接口实现类(素材)
 * @author 姜友瑶
 * @date 2018-07-18 07:20
 */
@Service
public class  BizMaterialServiceImpl implements  BizMaterialService{

	
	@Autowired
	private BizMaterialDao bizMaterialDao;
	
	
	@Override
	public int add(BizMaterial bizMaterial){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizMaterial.setCreateBy(loginUser.getSuName());
		bizMaterial.setUpdateBy(loginUser.getSuName());
		bizMaterial.setId(UUIDUtil.getRandomID());
		return bizMaterialDao.insert(bizMaterial);
		
	}
	
	@Override
	public int batchAdd(List<BizMaterial>  bizMaterialList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizMaterialList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizMaterialList.size() ? bizMaterialList.size() : end;
			List<BizMaterial> insertList = bizMaterialList.subList(begin, end);
			num += bizMaterialDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizMaterial oldBizMaterial
	,BizMaterial newBizMaterial){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizMaterial, newBizMaterial)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizMaterial, newBizMaterial);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizMaterial);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("id", oldBizMaterial.getId());
			bizMaterialDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizMaterial bizMaterial){
	
		return bizMaterialDao.updateByModel(bizMaterial);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizMaterialDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String id){
	
		return bizMaterialDao.deleteById(id);
	
	}
	
	@Override
	public int removeByModel(BizMaterial bizMaterial){
	
		return bizMaterialDao.deleteByModel(bizMaterial);
	
	}
	
	
	@Override
	public List<BizMaterial> findInPage(BizMaterial bizMaterial,  PaginationVO pageVo){
	
		return bizMaterialDao.selectInPage(bizMaterial , pageVo);
	
	}
	
	@Override
	public List<BizMaterial> findByModel(BizMaterial bizMaterial){
	
		return bizMaterialDao.selectByModel(bizMaterial);
	
	}
	
	@Override
	public int  findTotal(BizMaterial bizMaterial){
	
		return bizMaterialDao.selectTotalRecord(bizMaterial);
	
	}
	
	@Override
	public BizMaterial  findById(String id){
	
		return bizMaterialDao.selectById(id);
	
	}

   	
	
	
}